package com.jxw.launcher.util;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

/* loaded from: classes.dex */
public class ImageUtil {
    public static Bitmap createBitmap(Bitmap bitmap, String str) {
        Rect rect;
        if (bitmap == null) {
            return null;
        }
        Bitmap createBitmap = Bitmap.createBitmap(bitmap.getWidth() + 0, bitmap.getHeight() + 0, Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(createBitmap);
        float f = 0;
        canvas.drawBitmap(bitmap, f, f, (Paint) null);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(-1);
        paint.setTextSize(18.0f);
        paint.getTextBounds(str, 0, str.length(), new Rect());
        canvas.drawText(str, createBitmap.getWidth() - rect.width(), createBitmap.getHeight() - rect.height(), paint);
        return createBitmap;
    }

    public static Bitmap decodeByteArray(byte[] bArr) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;
        options.inJustDecodeBounds = false;
        options.inPurgeable = true;
        options.inInputShareable = true;
        return BitmapFactory.decodeByteArray(bArr, 0, bArr.length, options);
    }

    public static Bitmap decodeResource(Resources resources, int i) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;
        options.inJustDecodeBounds = false;
        options.inPurgeable = true;
        options.inInputShareable = true;
        return BitmapFactory.decodeResource(resources, i, options);
    }

    public static Bitmap decodeResource(Resources resources, int i, int i2, int i3) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(resources, i, options);
        options.inSampleSize = calculateInSampleSize(options, i2, i3);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(resources, i, options);
    }

    public static int calculateInSampleSize(BitmapFactory.Options options, int i, int i2) {
        int i3 = options.outHeight;
        int i4 = options.outWidth;
        if (i3 > i2 || i4 > i) {
            int round = Math.round(i3 / i2);
            int round2 = Math.round(i4 / i);
            return round < round2 ? round : round2;
        }
        return 1;
    }

    public static void recycle(Drawable drawable) {
        if (drawable == null || !(drawable instanceof BitmapDrawable)) {
            return;
        }
        recycle(((BitmapDrawable) drawable).getBitmap());
    }

    public static void recycle(Bitmap bitmap) {
        if (bitmap == null || bitmap.isRecycled()) {
            return;
        }
        bitmap.recycle();
    }
}
